Long only 1/n portfolio#
import pandas as pd
pd.options.plotting.backend = "plotly"
import yfinance as yf
from cvx.simulator.builder import builder
from cvx.simulator.grid import resample_index
data = yf.download(tickers = "SPY AAPL GOOG MSFT", # list of tickers
period = "10y", # time period
interval = "1d", # trading interval
prepost = False, # download pre/post market hours data?
repair = True) # repair obvious price errors e.g. 100x?
[ 0% ]
[**********************50% ] 2 of 4 completed
[**********************75%*********** ] 3 of 4 completed
[*********************100%***********************] 4 of 4 completed
prices = data["Adj Close"]
capital = 1e6
b = builder(prices=prices, initial_cash=capital)
for time, state in b:
# each day we invest a quarter of the capital in the assets
b[time[-1]] = 0.25 * state.nav / state.prices
portfolio = b.build()
portfolio.profit.cumsum().plot()
portfolio.nav.plot()
Rebalancing#
Usually we would not execute on a daily basis but rather rebalance every week, month or quarter. There are two approaches to deal with this problem in cvxsimulator.
Resample the existing daily portfolio (helpful to see effect of your hesitated trading)
Trade only on days that are within a predefined grid (most flexible if you have a rather irregular grid)
Resample an existing portfolio#
portfolio_resampled = portfolio.resample(rule="M")
frame = pd.DataFrame({"original": portfolio.nav, "monthly": portfolio_resampled.nav})
frame
| original | monthly | |
|---|---|---|
| Date | ||
| 2013-06-27 | 1.000000e+06 | 1.000000e+06 |
| 2013-06-28 | 1.001085e+06 | 1.001085e+06 |
| 2013-07-01 | 1.011391e+06 | 1.011441e+06 |
| 2013-07-02 | 1.012207e+06 | 1.012257e+06 |
| 2013-07-03 | 1.015417e+06 | 1.015482e+06 |
| ... | ... | ... |
| 2023-06-20 | 8.237944e+06 | 8.219519e+06 |
| 2023-06-21 | 8.145281e+06 | 8.127688e+06 |
| 2023-06-22 | 8.267656e+06 | 8.249011e+06 |
| 2023-06-23 | 8.205775e+06 | 8.187303e+06 |
| 2023-06-26 | 8.077051e+06 | 8.060225e+06 |
2516 rows × 2 columns
print(portfolio_resampled.stocks)
AAPL GOOG MSFT SPY
Date
2013-06-27 20475.101972 11444.334730 8665.696774 1864.597492
2013-06-28 20475.101972 11444.334730 8665.696774 1864.597492
2013-07-01 19927.001781 11433.775501 8830.725975 1882.564934
2013-07-02 19927.001781 11433.775501 8830.725975 1882.564934
2013-07-03 19927.001781 11433.775501 8830.725975 1882.564934
... ... ... ... ...
2023-06-20 11210.476908 16232.971784 6070.403593 4803.930843
2023-06-21 11210.476908 16232.971784 6070.403593 4803.930843
2023-06-22 11210.476908 16232.971784 6070.403593 4803.930843
2023-06-23 11210.476908 16232.971784 6070.403593 4803.930843
2023-06-26 11210.476908 16232.971784 6070.403593 4803.930843
[2516 rows x 4 columns]
# almost hard to see that difference between the original and resampled portfolio
frame.plot()
# number of shares traded
portfolio_resampled.trades_stocks.iloc[1:].plot()
Trade only days in predefined grid#
b = builder(prices=prices, initial_cash=capital)
# define a grid
grid = resample_index(prices.index, rule="M")
for time, state in b:
# each day we invest a quarter of the capital in the assets
if time[-1] in grid:
b[time[-1]] = 0.25 * state.nav / state.prices
else:
# forward fill an existing position
b[time[-1]] = b[time[-2]]
portfolio = b.build()
portfolio.nav.plot()
# Trading only once a month can lead to days where 150k had to be reallocated
portfolio.turnover.iloc[1:].plot()
Why not resampling the prices?#
I don’t believe in bringing the prices to a monthly grid. This would render it hard to construct signals given the sparse grid. We stay on a daily grid and trade once a month.